package tech.aistar.prj.service.impl;

import org.apache.ibatis.session.SqlSession;
import tech.aistar.prj.mapper.UserMapper;
import tech.aistar.prj.mapper.VideoMapper;
import tech.aistar.prj.model.dto.VideoDTO;
import tech.aistar.prj.model.pojo.User;
import tech.aistar.prj.model.pojo.Video;
import tech.aistar.prj.service.IVideoService;
import tech.aistar.prj.util.MybatisUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class VideoServiceImpl implements IVideoService {
    @Override
    public List<VideoDTO> findVideoAndUsername() {
        SqlSession sqlSession = null;
        List<VideoDTO> list = new ArrayList<>();
        try {
            sqlSession = MybatisUtil.getSession();
            VideoMapper videoMapper = sqlSession.getMapper(VideoMapper.class);
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            List<Video> videoList = videoMapper.findAll();
            for (Video video : videoList) {
                VideoDTO dto = new VideoDTO();
                dto.setVideo(video);
                User u = userMapper.findById(video.getUserId());
                dto.setUsername(u.getUsername());
                list.add(dto);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<VideoDTO> findByConditions(String username, Double score, Integer likes, String title) {
        SqlSession sqlSession = null;
        List<VideoDTO> list = new ArrayList<>();
        try {
            sqlSession = MybatisUtil.getSession();
            VideoMapper videoMapper = sqlSession.getMapper(VideoMapper.class);
            list =videoMapper.getByUsernameOrScoreOrlikesOrTitle(username,score,likes,title);

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            MybatisUtil.close(sqlSession);
        }
        return list;
    }


    @Override
    public List<VideoDTO> rise(List<VideoDTO> list) {
        Collections.sort(list, new Comparator<VideoDTO>() {
            @Override
            public int compare(VideoDTO o1, VideoDTO o2) {
                if (o1.getVideo().getLikes()>o2.getVideo().getLikes()){
                    return 1;
                }else return -1;
            }
        });
        return list;
    }

    @Override
    public List<VideoDTO> desc(List<VideoDTO> list) {
        Collections.sort(list, new Comparator<VideoDTO>() {
            @Override
            public int compare(VideoDTO o1, VideoDTO o2) {
                if (o1.getVideo().getLikes()>o2.getVideo().getLikes()){
                    return -1;
                }else return 1;
            }
        });
        return list;
    }

    @Override
    public List<Video> searchByUser(String username) {
        SqlSession sqlSession = null;
        VideoMapper videoMapper = null;
        List<Video> list = null;
        try {
            sqlSession = MybatisUtil.getSession();
            videoMapper = sqlSession.getMapper(VideoMapper.class);
            list = videoMapper.findByUsername(username);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            MybatisUtil.close(sqlSession);
        }
        return list;
    }

    @Override
    public int addVideo(Video v) {
        SqlSession sqlSession = null;
        VideoMapper videoMapper = null;
        int count = 0;
        try {
            sqlSession = MybatisUtil.getSession();
            videoMapper = sqlSession.getMapper(VideoMapper.class);
            count = videoMapper.userAddVideo(v);
            sqlSession.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            MybatisUtil.close(sqlSession);
        }
        return count;
    }

    @Override
    public int delVideo(Integer id) {
        SqlSession sqlSession = null;
        VideoMapper videoMapper = null;
        int count = 0;
        try {
            sqlSession = MybatisUtil.getSession();
            videoMapper = sqlSession.getMapper(VideoMapper.class);
            count = videoMapper.delVideo(id);
            sqlSession.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            MybatisUtil.close(sqlSession);
        }
        return count;
    }

    @Override
    public int releaseVideo(Integer id) {
        SqlSession sqlSession = null;
        VideoMapper videoMapper = null;
        int count = 0;
        try {
            sqlSession = MybatisUtil.getSession();
            videoMapper = sqlSession.getMapper(VideoMapper.class);
            count = videoMapper.releaseVideo(id);
            sqlSession.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            MybatisUtil.close(sqlSession);
        }
        return count;
    }

    @Override
    public List<Video> descByStatusAndTime(List<Video> list) {
       Collections.sort(list, new Comparator<Video>() {
           @Override
           public int compare(Video v1, Video v2) {
               if (v1.getStatus()>v2.getStatus()){
                   return -1;
               }else if (v1.getStatus()==v2.getStatus()){
                   if (v2.getCreateDate().compareTo(v1.getCreateDate())>0){
                       return 1;
                   }else return -1;
               }else if (v1.getStatus()<v2.getStatus()){
                   return 1;
               }else return 0;
           }
       });
       return list;
    }

    @Override
    public Video findByVid(Integer Vid) {
        SqlSession sqlSession = null;
        VideoMapper videoMapper = null;
        Video v = null;
        int count = 0;
        try {
            sqlSession = MybatisUtil.getSession();
            videoMapper = sqlSession.getMapper(VideoMapper.class);
            v = videoMapper.findByUid(Vid);
            sqlSession.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            MybatisUtil.close(sqlSession);
        }
        return v;
    }

    @Override
    public int updateVideo(String title, String videoUrl, Integer likes, Double score, Integer status, Integer id) {
        SqlSession sqlSession = null;
        VideoMapper videoMapper = null;
        int count = 0;
        try {
            sqlSession = MybatisUtil.getSession();
            videoMapper = sqlSession.getMapper(VideoMapper.class);
            count = videoMapper.updateVideo(title,videoUrl,likes,score,status,id);
            sqlSession.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            sqlSession.commit();
            MybatisUtil.close(sqlSession);
        }
        return count;
    }

}
